<!--
 * @Descripttion: 退款管理
 * @Author: xiao li
 * @Date: 2020-07-06 12:17:07
 * @LastEditors: xiao li
 * @LastEditTime: 2022-01-24 14:53:14
-->
<template>
  <div class="lb-order-manage">
    <top-nav />
    <div class="page-main">
      <div class="page-search-form">
        <el-form
          @submit.native.prevent
          :inline="true"
          :model="searchForm"
          ref="searchForm"
        >
          <el-form-item label="订单状态" prop="order_status">
            <el-select
              @change="getTableDataList(1)"
              v-model="searchForm.order_status"
              placeholder="请选择"
            >
              <el-option
                v-for="item in statusOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="订单号" prop="transaction_id">
            <el-input
              v-model="searchForm.transaction_id"
              placeholder="请输入付款订单号"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <lb-button
              size="medium"
              type="primary"
              icon="el-icon-search"
              style="margin-right: 5px"
              @click="getTableDataList(1)"
              >{{ $t('action.search') }}</lb-button
            >
            <lb-button
              size="medium"
              icon="el-icon-refresh-left"
              style="margin-right: 5px"
              @click="resetForm('searchForm')"
              >{{ $t('action.reset') }}</lb-button
            >
          </el-form-item>
        </el-form>
      </div>
      <el-table
        v-loading="loading"
        :data="tableData"
        :header-cell-style="{ background: '#f5f7fa', color: '#606266' }"
        style="width: 100%"
      >
        <el-table-column prop="goods_info_text" width="300" label="商品信息">
          <template slot-scope="scope">
            <div
              class="table-goods-info"
              v-for="(item, index) in scope.row.order_goods_text"
              :key="index"
            >
              <lb-image :src="item.cover" />
              <div class="goods-info-r">
                <p>{{ item.name }}</p>
                <div>
                  <span class="price"
                    >￥{{ (item.price / item.number).toFixed(2) }}</span
                  >
                  <span class="count">x {{ item.number }}</span>
                </div>
                <p>规格：{{ item.content }}</p>
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          prop="user_id"
          label="用户ID"
          width="80"
        ></el-table-column>
        <el-table-column prop="nickName" label="客户昵称"></el-table-column>
        <el-table-column prop="name" label="员工" width="120"></el-table-column>
        <el-table-column
          prop="price"
          label="总金额"
          width="100"
        ></el-table-column>
        <el-table-column
          prop="total_price"
          label="实付金额"
          width="100"
        ></el-table-column>
        <el-table-column
          prop="refund_price"
          label="退款金额"
          width="100"
        ></el-table-column>
        <el-table-column prop="status" label="状态">
          <template slot-scope="scope">
            <span>{{ statusType[scope.row.status] }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="phone" label="支付方式">
          <template slot-scope="scope">
            <div>
              {{
                scope.row.help_pay === 1
                  ? '好友代付'
                  : scope.row.stored * 1 > 0
                  ? '储值支付'
                  : '微信支付'
              }}
            </div>
          </template>
        </el-table-column>
        <el-table-column
          prop="transaction_id"
          width="150"
          label="付款订单号"
        ></el-table-column>
        <el-table-column
          prop="out_refund_no"
          width="150"
          label="退款订单号"
        ></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <div class="table-operate" v-if="scope.row.status === 1">
              <lb-button
                type="primary"
                size="mini"
                plain
                @click="showRefundDialog(scope.row.id, scope.row.total_price)"
                >立即退款</lb-button
              >
              <lb-button
                type="danger"
                size="mini"
                plain
                @click="toRefuse(scope.row.id)"
                >拒绝退款</lb-button
              >
            </div>
          </template>
        </el-table-column>
      </el-table>
      <lb-page
        :batch="false"
        :page="searchForm.page"
        :pageSize="searchForm.limit"
        :total="total"
        @handleSizeChange="handleSizeChange"
        @handleCurrentChange="handleCurrentChange"
      ></lb-page>
    </div>

    <el-dialog title="退款" :visible.sync="dialogRefund" width="400px" center>
      <div class="refund-inner">
        <lb-tips>请核对信息后输入需要退款的金额</lb-tips>
        <el-input
          :disabled="refundTotalMoney * 1 === 0"
          v-model="refundMoney"
          placeholder="请输入退款金额"
        ></el-input>

        <div class="mt-md f-caption c-caption">
          <div>
            实际支付的金额
            <span class="c-warning">￥{{ refundTotalMoney }}</span>
          </div>
          <div class="mt-sm">退款金额不能大于订单实际支付金额</div>
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogRefund = false">{{
          $t('action.cancel')
        }}</el-button>
        <el-button type="primary" @click="toPassRefund">确认退款</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    return {
      loading: false,
      statusOptions: [
        {
          label: '全部',
          value: 0
        },
        {
          label: '退款中',
          value: 1
        },
        {
          label: '退款成功',
          value: 2
        },
        {
          label: '退款失败',
          value: 3
        }
      ],
      statusType: {
        1: '退款中',
        2: '退款成功',
        3: '退款失败'
      },
      searchForm: {
        page: 1,
        limit: 10,
        order_status: 0,
        transaction_id: ''
      },
      tableData: [],
      total: 0,
      dialogRefund: false,
      refundId: '',
      refundMoney: '',
      refundTotalMoney: '',
      lockRefund: false
    }
  },
  created () {
    this.getTableDataList()
  },
  methods: {
    resetForm (name) {
      this.$refs[name].resetFields()
      this.getTableDataList(1)
    },
    handleSizeChange (val) {
      this.searchForm.limit = val
      this.handleCurrentChange(1)
    },
    handleCurrentChange (val) {
      this.searchForm.page = val
      this.getTableDataList()
    },
    getTableDataList (flag) {
      if (flag) this.searchForm.page = 1
      console.log(this.searchForm.page)
      this.loading = true
      let { searchForm } = this
      this.$api.refundList(searchForm).then(res => {
        this.loading = false
        if (res.code === 200) {
          this.tableData = res.data.data
          this.total = res.data.total
        }
      })
    },
    showRefundDialog (id, money) {
      this.refundId = id
      this.refundMoney = money
      this.refundTotalMoney = money
      this.dialogRefund = true
    },
    /**
     * @method: 同意退款
     */
    async toPassRefund () {
      if (this.lockRefund) return
      this.lockRefund = true
      let { refundId: id } = this
      let price = this.refundMoney * 1
      let refundTotalMoney = this.refundTotalMoney * 1
      let param = { id, refund_price: price }
      let reg = /^(([1-9][0-9]*)|(([0]\.\d{1,2}|[1-9][0-9]*\.\d{1,2})))$/
      if ((refundTotalMoney === 0 && price === 0) || (price > 0 && price <= refundTotalMoney && reg.test(price))) {
        let { code } = await this.$api.settingRefundInfo(param)
        this.lockRefund = false
        if (code !== 200) return
        this.$message.success(this.$t('tips.successSub'))
        this.dialogRefund = false
        this.refundMoney = ''
        this.getTableDataList()
      } else {
        this.lockRefund = false
        this.$message.error('请核对金额再提交！')
      }
    },
    /**
     * @method: 拒绝退款
     */
    toRefuse (id) {
      this.$confirm(this.$t('tips.confirmNoRefund'), this.$t('tips.reminder'), {
        confirmButtonText: this.$t('action.comfirm'),
        cancelButtonText: this.$t('action.cancel'),
        type: 'warning'
      }).then(() => {
        this.$api.cancelRefund({ id }).then(res => {
          if (res.code === 200) {
            this.$message.success(this.$t('tips.successSub'))
            this.getTableDataList()
          }
        })
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.lb-order-manage {
  .page-main {
    width: 100%;
    padding: 20px;
  }
  .el-form {
    .el-select,
    .el-input-number,
    .el-input {
      width: 200px;
    }
  }
  .el-table {
    .table-goods-info {
      width: 280px;
      height: 80px;
      display: flex;
      align-items: center;
      margin-bottom: 10px;
      font-size: 12px;

      .goods-info-r {
        width: 100%;
        display: flex;
        flex-direction: column;
        justify-content: space-around;

        p {
          width: 210px;
          white-space: nowrap;
          overflow: hidden;
          text-overflow: ellipsis;
        }

        .price {
          color: red;
        }
      }

      .el-image {
        min-width: 70px;
        height: 70px;
        margin-right: 10px;
      }

      &:last-child {
        margin-bottom: 0;
      }
    }
  }
  .refund-inner {
    width: 100%;
    .el-input {
      margin: 16px 0;
    }
    p {
      margin-bottom: 16px;
    }
    span {
      color: red;
    }
  }
}
</style>
